<?php echo $this->render('/public/header'); ?>
<script src="https://cdn.bootcss.com/echarts/3.8.5/echarts.min.js"></script>
<?php use yii\widgets\LinkPager; ?>
<?php use yii\helpers\Url; ?>
<?php $default = yii::$app->request->get('pay_type') ? yii::$app->request->get('pay_type') : 0; ?>
<div id="main" class="col-xs-12">
    <form action="" method="get">
        <div class="form-inline">
            <div class="form-group">
                <select class="form-control" style="width:100px;padding:0px" name="pay_type"
                        v-model="income_type_selected">
                    <option value="0">收款方式</option>
                    <option v-for="(v,k) in income_type" :value="v.id">{{v.name}}</option>
                </select>
            </div>
            <div class="form-group">
                <input type="text" name="begin_time" class="layui-input"
                       placeholder="起始时间"
                       style="width:150px"
                       value="<?php echo yii::$app->request->get('begin_time'); ?>"
                       onclick="layui.laydate({elem: this})">
            </div>
            <div class="form-group">
                <input type="text" name="end_time" class="layui-input"
                       style="width:150px"
                       placeholder="结束时间"
                       value="<?php echo yii::$app->request->get('end_time'); ?>"
                       onclick="layui.laydate({elem: this})">
            </div>
            <div class="form-group">
                <select class="form-control" name="property_p">
                    <option value="0">请选择性质</option>
                    <?php foreach ($data['property'] as $v): ?>
                        <option value="<?php echo $v['id']; ?>"<?php echo yii::$app->request->get('property_p') == $v['id'] ? 'selected' : ''; ?>><?php echo $v['name']; ?></option>
                    <?php endforeach; ?>
                    <option value="999">未选择性质</option>
                </select>
            </div>
            <div class="form-group">
                <input type="text" style="width:150px" name="invocie" class="form-control"
                       value="<?php echo yii::$app->request->get('invocie'); ?>" placeholder="搜索invocie编号">
            </div>

            <div class="form-group">
                <input type="text" style="width:150px" name="number" class="form-control"
                       value="<?php echo yii::$app->request->get('number'); ?>" placeholder="搜索客户编号">
            </div>
            <div class="form-group">
                <input type="text" style="width:150px" name="customer_name" class="form-control"
                       value="<?php echo yii::$app->request->get('customer_name'); ?>" placeholder="搜索客户">
            </div>
            <div class="form-group">
                <select class="form-control" name="service_name" v-model="staff_selected">
                    <option value=0>--请选择业务员--</option>
                    <option v-for="(v,k) in staff_list" :value="v.job_number">{{v.name}}</option>
                </select>
            </div>
            <button class="btn btn-success btn-small" style="margin-left: 5px">搜索</button>


            <div class="alert alert-danger" style="display: inline-block">
                累计收入: <br/><?php echo $data['caculate']['in_come']; ?>
            </div>
            <div class="alert alert-warning" style="display: inline-block">
                累计费用: <br/><?php echo $data['caculate']['fee']; ?>
            </div>
            <div class="alert alert-info" style="display: inline-block">
                累计支出: <br/><?php echo $data['caculate']['total_fee']; ?>
            </div>
            <div class="alert alert-success" style="display: inline-block">
                累计净额:
                <br/><?php echo($data['caculate']['in_come'] + $data['caculate']['total_fee'] + $data['caculate']['fee']); ?>
            </div>
        </div>

    </form>
    <div class="row">
        <div class="col-lg-3">
            <a class="btn btn-success" target="_blank" href="javascript:downloadExcel()">下载excel</a>
            <button class="btn btn-danger" @click="showchart()">显示图表</button>
        </div>
        <div class="col-lg-3 col-lg-offset-6">
            <div class="label label-danger" style="display: inline-block">
                paypal余额: <br/><?php echo $data['account'][1]['money']; ?>
            </div>
            <div class="label label-warning" style="display: inline-block">
                TT余额: <br/><?php echo $data['account'][2]['money']; ?>
            </div>
            <div class="label label-info" style="display: inline-block">
                西联余额: <br/><?php echo $data['account'][3]['money']; ?>
            </div>
            <div class="label label-info" style="display: inline-block">
                邮政余额(rmb): <br/><?php echo $data['account'][4]['money']; ?>
            </div>
        </div>
    </div>
    <div id="table" v-show="!show">
        <table class="table table-bordered;">
            <thead>
            <tr>
                <th style="width:40px">id</th>
                <th style="width:80px">日期</th>
                <th style="width:50px">是否拆分</th>
                <th style="width:90px">性质</th>
                <th style="width:130px">交易对手方名称</th>

                <th style="width:100px">编号</th>
                <th style="width:90px">客户名称</th>
                <th style="width:50px">国家</th>
                <th style="width:80px">业务员</th>
                <th style="width:75px">类型</th>
                <th style="width:50px">币种</th>
                <th style="width:100px">invocie编号</th>
                <th style="width:70px">支出</th>
                <th>收入</th>
                <th style="width:70px">费用</th>
                <th style="width:80px">净额</th>
                <th style="width:80px">余额</th>
                <th>备注</th>
                <th style="width:80px">交易号</th>
                <th style="width:90px">支付来源</th>
                <th style="width:100px">操作</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($data['list'] as $k => $v) { ?>
                <tr>
                    <td><?php echo $v['id'];?></td>
                <td>
                    <input style="border:none;padding: 0px;width:100%"
                           onchange="update_income(<?php echo $v['id']; ?>,'date',this)"
                           type="text" value="<?php echo $v['date']; ?>"/>

                </td>
                <td><?php echo $v['pid'] != $v['id'] ? '拆分项父id'.$v['pid'] : ''; ?></td>
                <td>
                    <select style="border:none;padding:0px" class="form-control"
                            onchange="update_income(<?php echo $v['id']; ?>,'property_p',this)">
                        <option value="0">请选择</option>
                        <?php foreach ($data['property'] as $v2): ?>
                            <?php if ($v2['pid'] == 0): ?>
                                <option value="<?php echo $v2['id']; ?>" <?php if ($v['property_p'] == $v2['id']) {
                                    echo 'selected';
                                }; ?>><?php echo $v2['name']; ?></option>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </select>
                </td>

                <td>
                    <input style="border:none;padding: 0px;width:100%"
                           onchange="update_income(<?php echo $v['id']; ?>,'name',this)"
                           type="text" value="<?php echo $v['name']; ?>"/>
                </td>

                <td>
                    <input style="border:none;padding: 0px;width:100%;<?php echo $v['same'] ? '' : "color:red"; ?>"
                           onchange="update_income(<?php echo $v['id']; ?>,'number',this)"
                           type="text" value="<?php echo $v['number']; ?>"/>
                </td>
                <td>
                    <input style="border:none;padding: 0px;width:100%;<?php echo $v['same'] ? '' : "color:red"; ?>"
                           onchange="update_income(<?php echo $v['id']; ?>,'customer_name',this)"
                           type="text" value="<?php echo $v['customer_name']; ?>"/>
                </td>
                <td>
                    <input style="border:none;padding: 0px;width:100%"
                           onchange="update_income(<?php echo $v['id']; ?>,'country',this)"
                           type="text" value="<?php echo $v['country']; ?>"/>
                </td>

                <td>
                    <select style="border:none;padding: 0px;width:100%"
                            onchange="update_income(<?php echo $v['id']; ?>,'service_name',this)"
                    >
                        <option value="0">--请选择--</option>
                        <?php foreach ($data['staff'] as $k2 => $v2) { ?>
                            <option
                                    value="<?php echo $v2['job_number']; ?>"
                                <?php if ($v['service_name'] == $v2['job_number']) {
                                    echo "selected";
                                } ?>
                            ><?php echo $v2['name']; ?>
                            </option>
                        <?php } ?>
                    </select>
                </td>
                <td>
                    <select style="border:none;padding: 0px;width:100%"
                            onchange="update_income(<?php echo $v['id']; ?>,'pay_type',this)"
                    >
                        <option value=0>--请选择--</option>
                        <option value=1 <?php if ($v['pay_type'] == 1) {
                            echo 'selected';
                        } ?>>支出
                        </option>
                        <option value=2 <?php if ($v['pay_type'] == 2) {
                            echo 'selected';
                        } ?>>退款
                        </option>
                        <option value=3 <?php if ($v['pay_type'] == 3) {
                            echo 'selected';
                        } ?>>收款
                        </option>
                    </select>
                </td>
                <td>
                    <input style="border:none;padding: 0px;width:100%"
                           onchange="update_income(<?php echo $v['id']; ?>,'money_type',this)"
                           type="text" value="<?php echo $v['money_type']; ?>"/>
                </td>
                <td>
                    <input style="border:none;padding: 0px;width:100%"
                           onchange="update_income(<?php echo $v['id']; ?>,'invocie_num',this)"
                           type="text" value="<?php echo $v['invocie_num']; ?>"/>
                </td>
                <td>
                    <input style="border:none;padding: 0px;width:100%<?php if (!$v['moneySame']) {
                        echo ';color:red';
                    }; ?>"
                           onchange="update_income(<?php echo $v['id']; ?>,'total_fee',this)"
                           type="text" value="<?php echo $v['total_fee']; ?>"/>
                </td>
                <td>
                    <input style="border:none;padding: 0px;width:100%<?php if (!$v['moneySame']) {
                        echo ';color:red';
                    }; ?>"
                           onchange="update_income(<?php echo $v['id']; ?>,'in_come',this)"
                           type="text" value="<?php echo $v['in_come']; ?>"/>
                </td>

                <td>
                    <input style="border:none;padding: 0px;width:100%<?php if (!$v['moneySame']) {
                        echo ';color:red';
                    }; ?>"
                           onchange="update_income(<?php echo $v['id']; ?>,'fee',this)"
                           type="text" value="<?php echo $v['fee']; ?>"/>
                </td>
                <td>
                    <?php echo($v['in_come'] + $v['total_fee'] + $v['fee']); ?>
                </td>
                <td>
                <?php if ($v['pay_from'] == 1):?>
                    <input value=<?php echo $v['remain_money'];?> style="border:none;padding: 0px;width:100%" type="text" onchange="update_income(<?php echo $v['id']; ?>,'remain_money',this)" />
                <?php else: ?>
                <?php echo $v['remain_money'];?>
                <?php endif;?>
                    </td>
                    <td>
                        <input style="border:none;padding: 0px;width:100%"
                               onchange="update_income(<?php echo $v['id']; ?>,'remark',this)"
                               type="text" value="<?php echo $v['remark']; ?>"/>
                    </td>
                    <td><?php echo $v['email'];?></td>
                    <td>
                        <select style="border:none;padding: 0px;width:100%"
                                onchange="update_income(<?php echo $v['id']; ?>,'pay_from',this)"
                        >
                            <option value="0">请选择</option>
                            <?php foreach ($data['incomeType'] as $k2 => $v2) { ?>
                                <option
                                        value="<?php echo $v2['id']; ?>"
                                    <?php if ($v['pay_from'] == $v2['id']) {
                                        echo "selected";
                                    } ?>
                                ><?php echo $v2['name']; ?>
                                </option>
                            <?php } ?>
                        </select>
                    </td>
                    <td style="width:100px">
                        <a href="javascript:void(0);" onclick="del_income(<?php echo $v['id']; ?>)"
                           class="layui-btn layui-btn-mini layui-btn-danger">删除</a>
                        <a href="javascript:void(0);" onclick="split_income(<?php echo $v['id']; ?>)"
                           class="layui-btn layui-btn-mini layui-btn-danger">拆分</a>
                    </td>
                    </tr>
                    <?php } ?>

            </tbody>
        </table>
        <div class="form-inline">
            <div class="form-group">
                <?php echo LinkPager::widget(['pagination' => $pages]); ?>
            </div>

            <div style="display: inline-block;line-height: 55px">共<?php echo $data['size']; ?>条记录</div>
        </div>
    </div>
    <div v-show="show" ref="chart" style="width:1200px;height: 700px;"></div>

</div>
<script>

</script>
<script>
    new Vue({
        el: "#main",
        data: {
            income_type_selected:<?php echo yii::$app->request->get("pay_type") ?? 0;?>,
            income_type:<?php echo json_encode($data['incomeType']);?>,
            staff_selected:<?php echo yii::$app->request->get("service_name") ?? 0;?>,
            staff_list:<?php echo json_encode($data['staff']);?>,
            show: false,
            chart: ''
        },
        mounted: function () {
            var myChart = echarts.init(this.$refs.chart);
            // 指定图表的配置项和数据
            option = {
                color: ['#3398DB'],
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        data: <?php echo json_encode($data['title']);?>,
                        axisTick: {
                            alignWithLabel: true
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '直接访问',
                        type: 'bar',
                        barWidth: '60%',
                        data:<?php echo json_encode($data['chart']);?>
                    }
                ]
            };


            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
            this.chart = myChart;
        },
        methods: {
            showchart: function () {
                if (this.show == true) {
                    this.show = false;
                } else {
                    this.show = true;
                }
                console.log(this.chart);
                this.chart.resize();
            }
        }
    });


    function downloadExcel() {
        window.location.href = "/download/download-income" + location.search;
    }

    function update_income(id, column, e) {
        layer.confirm('确认修改', {}, function () {
            $.post('/table/income_edit_by_cloumn', {id: id, column: column, v: e.value}, function (res) {
                layer.confirm(res.msg, {}, function () {
                    window.location.reload();
                });
            }, 'json')
        });
    }

    function del_income(v) {
        layer.confirm('确认删除', {}, function () {
            var url = "<?php echo Url::to(['table/del_income']);?>";
            $.post(url, {id: v}, function (res) {
                if (res.code == 1) {
                    layer.confirm(res.msg, {}, function () {
                        window.location.reload();
                    })
                } else {
                    layer.alert(res.msg);
                }
            }, 'json')
        });
    }

    function split_income(id) {
        var url = "/api/split_income" + '?id=' + id;
        zeroModal.show({
            url: url
        })
    }
</script>
<?php echo $this->render('/public/footer'); ?>
